/*
 * Copyright (C) 2007  Vianney le Clément
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package fsahoraires.store.importers;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.*;
import static fsahoraires.util.DOMUtils.*;

import fsahoraires.cours.*;
import fsahoraires.cours.Horaire.Jour;
import fsahoraires.programmes.Programme;
import fsahoraires.programmes.Semestre;
import fsahoraires.store.XMLImporter;

public class XMLImporter014 implements XMLImporter {

	public String getDTD() {
		return "fsahoraires-0.1.4.dtd";
	}

	public Programme load(Document doc) {
		Programme prog = new Programme();
		CoursManager manager = prog.getManager();
		try {
			// lecture de la cache
			Element cache = getTag(doc, "cache");
			if (cache != null) {
				for (Element cached : getChildElements(cache)) {
					String code = cached.getAttribute("code");
					Cours cours = manager.getCours(code, false);
					cours.setTitre(getTagText(cached, "intitule"));
					cours.setAbreviation(getTagText(cached, "abreviation"));
					cours.setCredits(Float.parseFloat(getTagText(cached,
							"credits")));
					cours.setHeures(Float.parseFloat(getTagText(cached,
							"heures")));
					cours.setHoraire(1, parseHoraire(
							getTag(cached, "semestre1"), cours));
					cours.setHoraire(2, parseHoraire(
							getTag(cached, "semestre2"), cours));
				}
			}
			// lecture du programme
			Element programme = getTag(doc, "programme");
			for (Element s : getChildElements(programme)) {
				Semestre semestre = new Semestre(prog, Integer.parseInt(s
						.getAttribute("annee")), Integer.parseInt(s
						.getAttribute("semestre")));
				for (Element c : getChildElements(s))
					semestre.add(manager.getCours(c.getAttribute("code")));
			}
			return prog;
		} catch (Exception e) {
			e.printStackTrace();
			prog.close();
			return null;
		}
	}

	private Horaire parseHoraire(Element e, Cours cours) {
		CompoundHoraire horaire = new CompoundHoraire();
		for (Element occupation : getChildElements(e)) {
			Jour jour = Jour.valueOf(occupation.getAttribute("jour"));
			int tranche = Integer.parseInt(occupation.getAttribute("tranche")) - 1;
			String infos = occupation.getAttribute("infos");
			List<Integer> semaines = new ArrayList<Integer>();
			List<String> locaux = new ArrayList<String>();
			for (Element child : getChildElements(occupation)) {
				if (child.getNodeName().equals("semaine"))
					semaines.add(Integer
							.parseInt(child.getAttribute("semaine")));
				else
					locaux.add(getText(child));
			}
			horaire.add(new Occupation(cours, infos, locaux
					.toArray(new String[locaux.size()]), semaines), jour,
					tranche);
		}
		return horaire;
	}

}
